<template>
  <div class="home-container">
    <el-card style="margin-top: 10px;">
      <div style="display: flex;justify-content: space-between">
        <div >
          <el-button @click="handleAdd">发布</el-button>
        </div>
        <div style="display: flex">
          <div style="flex: 1">
            <el-input v-model="name" placeholder="请输入内容"></el-input>
          </div>
          <div style="flex: 1">
            <el-button style="margin-left: 10px;" @click="load">搜索</el-button>
            <el-button style="margin-left: 10px;" type="warning" @click="reset">重置</el-button>
          </div>
        </div>
      </div>
    </el-card>

    <div class="item-center">
      <el-row :gutter="24">
        <el-col :span="17">

          <el-row :gutter="10">
            <el-col :span="8" v-for="item in items" :key="item.id" style="margin-bottom: 10px">
              <div style="border: 1px solid rgb(255 255 255 / 80%); padding-bottom: 10px; border-radius: 10px; overflow: hidden; box-shadow: 0 2px 10px 0 rgb(0 0 0 / 10%); height: 340px;">
                <img :src="item.img" alt="" style="width: 100%; height: 220px;cursor: pointer ">
                <div style="color: #000; margin: 10px" class="info">名称:{{ item.name }}</div>
                <div style="margin: 10px;color: #8c939d;" class="info">简介:{{ item.info }}</div>
                <div style="margin: 10px;color: #8c939d;" class="info">状态:<span style="color:#af1818;">{{ item.status }}</span></div>
                <div style="display: flex;justify-content: space-around">
                  <el-button type="primary" @click="handleEdit(item)" v-if="item.status !== '可查看'"> 编辑 </el-button>
                  <el-button type="danger" @click="del(item.id)" > 删除 </el-button>
                </div>
              </div>
            </el-col>
          </el-row>
          <div style="margin: 10px 0">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[8 , 16]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
          </div>

          <el-dialog title="信息" :visible.sync="dialogFormVisible" width="70%" :close-on-click-modal="false">
            <el-form label-width="140px" size="small" style="width: 85%;" :model="form" :rules="rules" ref="ruleForm">
              <el-form-item prop="name" label="标题">
                <el-input v-model="form.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item prop="img" label="封面">
                <el-upload :action="serverHost+'/file/upload'" ref="img" :on-success="handleImgUploadSuccess">
                  <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
              </el-form-item>
              <el-form-item prop="info" label="详情">
                <mavon-editor ref="md" v-model="form.info" :ishljs="true" @imgAdd="imgAdd"/>
              </el-form-item>

              <el-form-item prop="file" label="文件">
                <el-upload :action="serverHost+'/file/upload'" :show-file-list="false"
                           :on-success="handleFileUploadSuccess" style="display: inline-block">
                  <el-button type="primary" plain class="ml-5">上传文件 <i class="el-icon-top"></i></el-button>
                </el-upload>
              </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="save">确 定</el-button>
            </div>
          </el-dialog>

        </el-col>

        <el-col :span="7">

          <el-card>
            <el-carousel height="300px" :interval="10000">
              <el-carousel-item v-for="item in imgs" :key="item">
                <img :src="item" style="width: 100%;height: 100%">
              </el-carousel-item>
            </el-carousel>
          </el-card>

        </el-col>
      </el-row>
    </div>

  </div>
</template>

<script>

import {serverHost} from "../../../config/config.default";
import axios from "axios";

export default {
  name: "",
  data() {
    return {
      items:[],
      name:'',
      pageNum:1,
      pageSize:8,
      total:0,
      form: {},
      serverHost : serverHost,
      categorys:[],
      dialogFormVisible: false,
      imgs: [
        "https://demosc.chinaz.net/Files/DownLoad/webjs1/201907/jiaoben6919/img/lunbo1.jpg",
        "https://pic2.zhimg.com/v2-d0e775ca666f3f2b049de8a88fc4aba5_r.jpg",
        "https://ts1.cn.mm.bing.net/th/id/R-C.bd3b631bde86bfdd75720dd544e3d32f?rik=IxwYcffHn4f3tQ&riu=http%3a%2f%2fimg.zcool.cn%2fcommunity%2f01f6625768ad220000012e7e78a909.jpg&ehk=TjQyehtL0i1DZVMZkrwUyBShDQclVX2ACNRZTTDKbzk%3d&risl=&pid=ImgRaw&r=0",
      ],
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur'}
        ],
      },
    };
  },
  created(){
    this.load()
  },
  methods:{
    imgAdd(pos, $file) {
      let $vm = this.$refs.md
      const formData = new FormData();
      formData.append('file', $file);
      axios({
        url: 'http://localhost:9090/file/upload',
        method: 'post',
        data: formData,
        headers: {'Content-Type': 'multipart/form-data'},
      }).then((res) => {
        $vm.$img2Url(pos, res.data);
      })
    },
    load(){
      this.request.get("/item/my/page",{
        params:{
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          name:this.name,
        }
      }).then(res=>{
        if(res.code==='200'){
          this.items=res.data?.records || []
          this.total=res.data?.total || 0
        }
      })
      this.request.get('/category').then(res=>{
        this.categorys=res.data
      })
    },
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.request.post("/item", this.form).then(res => {
            if (res.code === '200') {
              this.$message.success("保存成功")
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if(this.$refs.img) {
          this.$refs.img.clearFiles();
        }
        if(this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if(this.$refs.img) {
          this.$refs.img.clearFiles();
        }
        if(this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    del(id) {
      this.request.delete("/item/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    reset(){
      this.name=''
      this.load()
    }
  }
};
</script>

<style lang="scss" scoped>
.text {
  font-size: 14px;
}
.item {
  margin-bottom: 18px;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.info{
  height: 15px;
  line-height: 1.2;
  word-break: break-all;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1; /* 这里是超出几行省略 */
  overflow: hidden;
}

.home-container {
  width: 70%;
  margin: 0 auto;
  .item-center{
    .el-divider--horizontal {
      display: block;
      height: 1px;
      width: 100%;
      margin: 10px 0;
    }
    margin-top: 20px;
  }
}
</style>
